package com.boang.service;

import com.boang.entity.Cla;
import com.boang.util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/*
  @author: 13813
  @date: 2024/12/10 16:26
*/
public class ClaService {
    public int addCla(Cla cla) throws SQLException {
        Connection conn = DbUtil.getConn();
        PreparedStatement preparedStatement = conn.prepareStatement("insert into cla(name)values(?)");
        preparedStatement.setString(1, cla.getName());
        return preparedStatement.executeUpdate();
    }
    public int removeCla(int id) throws SQLException {
        Connection conn = DbUtil.getConn();
        PreparedStatement preparedStatement = conn.prepareStatement("delete from cla where id = ?");
        preparedStatement.setInt(1, id);
        return preparedStatement.executeUpdate();
    }
    public int updetaCla(Cla cla) throws SQLException {
        Connection conn = DbUtil.getConn();
        PreparedStatement preparedStatement = conn.prepareStatement("update cla set name=? where id = ?");
        preparedStatement.setString(1, cla.getName());
        preparedStatement.setInt(2, cla.getId());
        return preparedStatement.executeUpdate();
    }
    public List<Cla> listCla() throws SQLException {
        Connection conn = DbUtil.getConn();
        PreparedStatement preparedStatement = conn.prepareStatement("select * from cla");
        ResultSet resultSet = preparedStatement.executeQuery();
        ArrayList<Cla> list = new ArrayList<>();
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            Cla c = new Cla(id, name);
            list.add(c);
        }
        return list;
    }
}
